package org.join.ws.ui;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import org.join.web.serv.R;
import org.join.ws.WSApplication;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.chukong.sdk.util.CommonUtil;

public class WebServerEnter extends Activity implements OnClickListener {

    private Button mWebServerNoHotpot;
    private Button mWebServerWithHotpot;
    private List<File> mMovableList = null;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webserver_enter);
        WSApplication.getInstance().startWsService();
        mWebServerNoHotpot = (Button) findViewById(R.id.webserver_no_hotpot);
        mWebServerNoHotpot.setOnClickListener(this);
        mWebServerWithHotpot = (Button) findViewById(R.id.webserver_with_hotpot);
        mWebServerWithHotpot.setOnClickListener(this);
        View view = findViewById(R.id.moveapktogames);
        view.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch(v.getId()) {
        case R.id.webserver_no_hotpot:
            intent = new Intent(this, WebServerNoHotpot.class);
            startActivity(intent);
            break;
        case R.id.webserver_with_hotpot:
            intent = new Intent(this, WebServerWithHotpot.class);
            startActivity(intent);
            break;
        case R.id.moveapktogames:
            moveApks();
            break;
        default:
            break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        WSApplication.getInstance().stopWsService();
    }

    private void moveApks() {
        mProgressDialog = ProgressDialog.show(this, null, "查找文件中");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
        new Thread(){
            public void run() {
                mMovableList = new ArrayList<File>();
                File externalDir = Environment.getExternalStorageDirectory();
                list(externalDir);
                updateDialogMessage("移动文件中");
                mv();
                dismissDialog();
            }
        }.start();
    }

    private void list(File dir) {
        if (dir != null) {
            File[] files = dir.listFiles(); // 目录列表
            if (files != null) {
                for (File f : files) {
                    if (f.isFile() && f.getName().endsWith(".apk")
                            && !f.getAbsolutePath().startsWith(CommonUtil.getSingleton().getGameDir())) {
                        mMovableList.add(f);
                    } else {
                        list(f);
                    }
                }
            }
        }
    }

    private void mv() {
        String gameDir = CommonUtil.getSingleton().getGameDir();
        if (mMovableList == null || mMovableList.size() <= 0) {
            return;
        }
        String name = null;
        File nameTo = null;
        for (File f : mMovableList) {
            name = f.getName();
            nameTo = new File(gameDir, name);
            f.renameTo(nameTo);
        }
    }

    private void dismissDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                }
            }
        });
    }

    private void updateDialogMessage(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.setMessage(msg);
                }
            }
        });
    }

    private FileFilter mFilter = new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            if (pathname.getName().endsWith(".apk")) {
                return true;
            }
            return false;
        }
    };
}
